<?php

/**
 * This is the model class for table "dnd_boxanhfood_product".
 *
 * The followings are the available columns in table 'dnd_boxanhfood_product':
 * @property string $id
 * @property string $name
 * @property string $description
 * @property string $alias
 * @property integer $in_stock
 * @property integer $on_sale
 * @property string $on_sale_start
 * @property string $on_sale_end
 * @property string $creation_datetime
 * @property string $last_update
 * @property integer $status
 * @property string $category_id
 * @property string $image
 * @property string $banner
 */
class Product extends CActiveRecord
{
    public $image_;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'dnd_boxanhfood_product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('in_stock, status, on_sale', 'numerical', 'integerOnly'=>false),
			array('name, alias, code, image, banner', 'length', 'max'=>255),
			array('category_id', 'length', 'max'=>20),
			array('description, on_sale_start, on_sale_end, creation_datetime, last_update', 'safe'),
            array('name','required'),
            array('image_', 'file', 'types'=>'jpg, gif, png','allowEmpty' => true, 'on'=>'update'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, description, alias, in_stock, on_sale, on_sale_start, on_sale_end, creation_datetime, last_update, status, category_id, image, banner', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
        switch($this->scenario){
        case 'admin':
            return array(
                'id' => 'Định danh',
                'name' => 'Tên sản phẩm',
                'description' => 'Mô tả',
				'code' => 'Mã sản phẩm',
                'alias' => 'Tên rút gọn',
				'packing' => 'Đóng gói',
                'in_stock' => 'SL có',
                'on_sale' => 'Giá bán (VNĐ)',
                'on_sale_start' => 'Ngày bán',
                'on_sale_end' => 'Ngày ngưng bán',
                'creation_datetime' => 'Ngày tạo',
                'last_update' => 'Lập cập nhật cuối',
                'status' => 'Trạng thái',
                'category_id' => 'Loại sản phẩm',
                'image' => 'Hình ảnh sản phẩm',
                'banner' => 'Banner',
            );
            break;
            default:
                return array(
                    'id' => yii::t('model_product', 'id'),
                    'name' => yii::t('model_product', 'name'),
                    'description' => yii::t('model_product', 'description'),
                    'alias' => yii::t('model_product', 'alias'),
                     'in_stock' => yii::t('model_product', 'in_stock'),
                    'on_sale' => yii::t('model_product', 'on_sale'),
                    'on_sale_start' => yii::t('model_product', 'on_sale_start'),
                    'on_sale_end' => yii::t('model_product', 'on_sale_end'),
                    'creation_datetime' => yii::t('model_product', 'creation_datetime'),
                    'last_update' => yii::t('model_product', 'last_update'),
                    'status' => yii::t('model_product', 'status'),
                    'category_id' => yii::t('model_product', 'category_id'),
                    'image' => yii::t('model_product', 'image'),
                    'banner' => yii::t('model_product', 'banner'),
                    'code' => yii::t('model_product', 'code'),
                    'packing' => yii::t('model_product', 'packing'),
                    'number_of' => yii::t('model_product', 'number_of')
                );
            break;
        }
            
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('in_stock',$this->in_stock);
		$criteria->compare('on_sale',$this->on_sale);
		$criteria->compare('on_sale_start',$this->on_sale_start,true);
		$criteria->compare('on_sale_end',$this->on_sale_end,true);
		$criteria->compare('creation_datetime',$this->creation_datetime,true);
		$criteria->compare('last_update',$this->last_update,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('banner',$this->banner,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
